<?php

/**
 * This is the model class for table "baiviet".
 *
 * The followings are the available columns in table 'baiviet':
 * @property integer $id
 * @property integer $dmnhombv_id
 * @property string $ten_bv
 * @property string $link
 * @property string $img
 * @property string $mieu_ta
 * @property string $noi_dung
 * @property string $title
 * @property string $description
 * @property string $tag
 * @property string $ngay_tao
 * @property string $ngay_sua
 * @property integer $is_new
 * @property integer $is_active
 * @property integer $is_home
 * @property integer $is_about
 * @property integer $stt
 *
 * The followings are the available model relations:
 * @property Nhombaiviet $dmnhombv
 */
class Baiviet extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'baiviet';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('dmnhombv_id, ten_bv, noi_dung', 'required'),
			array('dmnhombv_id, is_new, is_active, is_home, is_about, stt', 'numerical', 'integerOnly'=>true),
			array('ten_bv, link, img, title, description, tag', 'length', 'max'=>1000),
			array('mieu_ta', 'length', 'max'=>3000),
			array('ngay_tao, ngay_sua', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, dmnhombv_id, ten_bv, link, img, mieu_ta, noi_dung, title, description, tag, ngay_tao, ngay_sua, is_new, is_active, is_home, is_about, stt', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'dmnhombv' => array(self::BELONGS_TO, 'Nhombaiviet', 'dmnhombv_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'dmnhombv_id' => 'Nhóm Bài Viết',
			'ten_bv' => 'Tên Bài Viết',
			'link' => 'Link',
			'img' => 'Ảnh',
			'mieu_ta' => 'Miêu Tả',
			'noi_dung' => 'Nội Dung',
			'title' => 'Thẻ Title',
			'description' => 'Thẻ Description',
			'tag' => 'Keywords',
			'ngay_tao' => 'Ngày Tạo',
			'ngay_sua' => 'Ngày Sửa',
			'is_new' => 'Hiển thị danh sách nhóm',
			'is_active' => 'Cho phép hiển thị',
			'is_home' => 'Hiển thị trang chủ',
            'is_about' => 'Bài Viết giới thiệu',
			'stt' => 'Số Thứ Tự',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('dmnhombv_id',$this->dmnhombv_id);
		$criteria->compare('ten_bv',$this->ten_bv,true);
		$criteria->compare('link',$this->link,true);
		$criteria->compare('img',$this->img,true);
		$criteria->compare('mieu_ta',$this->mieu_ta,true);
		$criteria->compare('noi_dung',$this->noi_dung,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('tag',$this->tag,true);
		$criteria->compare('ngay_tao',$this->ngay_tao,true);
		$criteria->compare('ngay_sua',$this->ngay_sua,true);
		$criteria->compare('is_new',$this->is_new);
		$criteria->compare('t.is_active',$this->is_active);
		$criteria->compare('is_home',$this->is_home);
        $criteria->compare('is_about',$this->is_about);
		$criteria->compare('stt',$this->stt);
        $criteria->with = array(
            "dmnhombv"=>array(
                'alias'=>'cat',
                'select'=>'cat.ten_nhom'
            )
        );

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
            ),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Baiviet the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    protected function afterFind()
    {
        // convert to display format
        //$this->ngay_tao= date('d-m-Y', strtotime( $this->ngay_tao));
        //$this->ngay_sua= date('d-m-Y', strtotime( $this->ngay_sua));
        parent::afterFind();
    }

    protected function beforeSave()
    {
        // convert to storage format
        $this->ngay_tao= date('Y-m-d', strtotime( $this->ngay_tao));
        $this->ngay_sua= date('Y-m-d', strtotime( $this->ngay_sua));
        return parent::beforeSave();
    }
}